/*
 * File      : i2c.h
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author                Notes
 * 2012-04-25     weety         first version
 */

#ifndef __I2C_H__
#define __I2C_H__

#include <rtthread.h>

#define TIMEOUT 0x10000
#define LONG_TIMEOUT (10 * TIMEOUT)

#define TIMEOUT_10_SEC	1000

#define INVALID	      	255
#define NORMAL_END      0
#define STOP_END        1
#define ERROR_END       2
#define COMERROR_END    3
#define TIMOUT_ERR      4



#define I2C1_USED

#ifdef 	I2C1_USED
#define I2C_1						  	I2C1
#define I2C1_CLK                      	RCC_APB1Periph_I2C1
#define I2C1_SCL_PIN                  	GPIO_Pin_6                  /* PB6 */
#define I2C1_SCL_GPIO_PORT            	GPIOB                       /* GPIOB */
#define I2C1_SCL_GPIO_CLK             	RCC_APB2Periph_GPIOB
#define I2C1_SCL_SOURCE               	GPIO_PinSource6
#define I2C1_SCL_AF                   	GPIO_Remap_I2C1
#define I2C1_SDA_PIN                 	GPIO_Pin_7                  /* PB7 */
#define I2C1_SDA_GPIO_PORT            	GPIOB                       /* GPIOB */
#define I2C1_SDA_GPIO_CLK             	RCC_APB2Periph_GPIOB
#define I2C1_SDA_SOURCE               	GPIO_PinSource7
#define I2C1_SDA_AF                  	GPIO_Remap_I2C1

#define I2C1_SPEED						100000						//100KHz
#define I2C1_SLAVE_ADDRESS7				0xE
#define I2C1_MODE 						I2C_Mode_I2C;
#define I2C1_DCL 						I2C_DutyCycle_2;
#define I2C1_ACK 						I2C_Ack_Enable;
#define I2C1_ACK_ADDRESS 				I2C_AcknowledgedAddress_7bit;
#endif

#define I2C2_SLAVE_ADDRESS7				0xF
#define I2C2_SPEED						100000						//100KHz


void I2C1_init(void);
void I2C2_init(void);
uint32_t I2C_single_write(I2C_TypeDef* I2Cx, uint8_t HW_address, uint8_t addr, uint8_t data);
void I2C_burst_write(I2C_TypeDef* I2Cx, uint8_t HW_address, uint8_t addr, uint8_t n_data, uint8_t *data);
uint8_t I2C_single_read(I2C_TypeDef* I2Cx, uint8_t HW_address, uint8_t addr);
void I2C_burst_read(I2C_TypeDef* I2Cx, uint8_t HW_address, uint8_t addr, uint8_t n_data, uint8_t *data);

uint32_t I2C_Write(uint8_t HW_address, uint8_t addr, uint8_t data);
uint32_t I2C_Read(uint8_t HW_address, uint8_t addr, uint8_t * data);


#endif
